<?php 
include 'includes/login_check.php';
//Gets the id of the challenge to view, if not there, redirect to home page
if (isset($_GET['challenge_id'])) {
	$challenge_id = $_GET['challenge_id'];	
} else {
    header("location: $home");
}

?>


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"><head>
	<?php include 'includes/head.php' ?>
	<link href="css/custom.css" rel="stylesheet" type="text/css" />
    <script language="javascript" src="js/battle.js"></script>
    <link rel="stylesheet" type="text/css" href="lightbox/lightbox.css" media="screen" />  
    <link rel="stylesheet" type="text/css" href="comment/css/style.css"  />

    <script type="text/javascript" src="lightbox/jquery.lightbox-0.5.js"></script>
    <script type="text/javascript">
			var challenge_id_var = <?php echo $challenge_id; ?>;
    </script>
    <script type="text/javascript" src="comment/js/comments.actions.js"></script>

	<script type="text/javascript">
    $(function() {
        $('.battle_item a').lightBox();
			$('.main-content').jqTransform({imgPath:'jqforms/img/'});
    });
    </script>
</head>

<body class="view_challenge">



<div id="fb-root"></div>
<script>
    (function(d, s, id) {
    var js, fjs = d.getElementsByTagName(s)[0];
    if (d.getElementById(id)) return;
    js = d.createElement(s); js.id = id;
    js.src = "//connect.facebook.net/en_US/all.js#xfbml=1";
    fjs.parentNode.insertBefore(js, fjs);
    }(document, 'script', 'facebook-jssdk'));
</script>

<div id="top">
    <div id="header">
        <?php include 'includes/nav_container.php' ?>
    </div>
</div>

<div id="wrapper">
    <table>
    	<tr>
        	<td id="col-left">
            	<?php include 'includes/current_activity.php'; ?>
            </td>
        	<td id="col-main">
            
            <?php 
			//Get contest info 
				$sqlquery=mysql_query("SELECT * FROM challenges WHERE id = '$challenge_id'");
				if (!$result) {
					 header("location: $home");

				}
				while($row = mysql_fetch_assoc($sqlquery)) {
					$title = $row['title'];
					$description = $row['description'];	
					$win_by_votes = $row['win_by_votes'];
					$win_by_time = $row['win_by_times'];
					$created_on = $row['created_on'];
					$end_time = $row['ended_on'];
					if ($row['private'] == '0') {
						$private = 'private';
					} else {
						$private = 'public';
					}
				}

			?>
            
            
            	<div>
                    <div class="main-title">
                        <h2>
                        	<span><?php echo $title ?></span>
                            <br class="cleft" />
                        </h2>
                    </div>
                    <div class="main-content">
                        <form class="" name="" id="" method="POST" action="vote_process.php">
                        <input type="hidden" name="challenge_id"  value="<?php echo $challenge_id ?>" /> 
                            <div class="block-left battlepage_container">
                                <ul>
                                   <?php
								   //get the contestants associated with the challenge
									$sqlquery=sprintf("SELECT * FROM contestants WHERE contestant_challenge_id = '$challenge_id' ORDER BY votes"); 
									$result = mysql_query($sqlquery) or die ("no query");
									$result_array = array();
									while($row = mysql_fetch_assoc($result))
									{
										$result_array[] = $row;
									}
									
									foreach ($result_array as $row) {
									
										$user_id = $row['contestant_user_id'];
										$contestant_username_query = mysql_query("SELECT username, oauth_uid FROM users WHERE id = '$user_id'");
										while ($row_getcontestantuser= mysql_fetch_assoc($contestant_username_query)) {
											$contestant_username = $row_getcontestantuser['username'];
											$contestant_oauth = $row_getcontestantuser['oauth_uid'];
										}
										
										
										$votes = $row['votes'];
										$contestant_id = $row['id'];
										$filepath = $row['image'];
										$cont_description = $row['description'];
									  ?>
                                      
									
                                   <li class="battle_item">
                                        <div>
                                            <input type="radio" name="vote" value="<?php  echo $contestant_id ?>" >
                                            <a href="<?php echo $filepath ?>" title="<?php echo $cont_description ?>" class="preview"></a>
                                            <p class="hover"><span><?php echo $votes ?><div class="popup" style="position:absolute;width:100px;height:200px;background-color: #45484d;border-radius:5px;-webkit-border-radius:5px;-moz-border-radius:5px; display:none;z-index:1;">
                                            	<p><span><img src="https://graph.facebook.com/<?php echo $contestant_oauth; ?>/picture?type=large" /><?php echo $contestant_username ?></span></p>
                                            </div></span></p>
                                            <!--Hover over votes, the contestant user info pops us-->
                                            
                                            <img src="<?php echo $filepath ?>" />
                                        </div>
                                    </li>
                                   
                                   <?php } ?>
                                    <li><br class="cleft" /></li>
                                </ul>
                                <div class="desc">
                                	<p>DESCRIPTION: <?php echo $description ?></p>
                                </div>
                                
                               
                                                                
                            </div>
                            <div class="block-right submit">
                                <input type="submit" value="">
                           <br /><br />
                                <h1>Challenge Info</h1><br />
                                <p><span>Challenge Ends: <?php 
								
								// contest end by votes and time	
								if (isset($win_by_votes) && isset($win_by_time) && $win_by_time != 0 && $win_by_votes != 0){
									echo "The challenge ends when either $win_by_votes votes is reached for a single contestant, or on $end_time, whichever comes first.";
								}
								
								// contest end by votes 	
								elseif (isset($win_by_votes) && (!isset($win_by_time) || $win_by_time == 0) && $win_by_votes != 0) {
									echo "The challenge ends when $win_by_votes votes is reached for a single contestant.";
								}
								
								// contest end by time 	
								else {
									echo "The challenge ends on $end_time.";
								}
								
								?></span></p><br />
                                <p><span>Public/Private: This contest is <?php echo $private ?></span></p><br />
                                 <?php
								//CHECK TO SEE IF CONTEST IS FULL ?>
                                <a href="join_contest.php?challenge_id=<?php echo $challenge_id ?>">Join Contest</a>
                                <ul>
                                	
									<?php 
									//List of contestants - votes / ordered by votes
									
									function array_sort_by_column(&$arr, $col, $dir = SORT_DESC) {
										$sort_col = array();
										foreach ($arr as $key=> $row) {
											$sort_col[$key] = $row[$col];
										}
									
										array_multisort($sort_col, $dir, $arr);
									}
									
									
									array_sort_by_column($result_array, 'votes');
									
									foreach ($result_array as $row) {
										 $user_id = $row['contestant_user_id'];

										//Get the user
										  $sqlquery_user=mysql_query("SELECT username FROM users WHERE  id = '$user_id'"); 
										  while ($row_getuser= mysql_fetch_assoc($sqlquery_user)) {
										  $user_name = $row_getuser['username'];
										  }
										  $votes = $row['votes'];
										?>
                                    <li>
                                    <?php echo $user_name ?> - <?php echo $votes ?>
                                    </li>
                                    <?php } ?>
                                
                                
                                </ul>
                              
                            </div>
                    </div>
                    </form>
                     <!--Comments-->
                                <div>
                                    <table id="displayComment" class="gridtable commentTable">
                                	
                                    </table>
                                </div>
                                <form id="newCommentForm">
                                    <table border="1" class="gridtable commentTable">
                                        <tr>
                                            <td><textarea name="newComment"></textarea></td>
                                        </tr>
                                        <tr>
                                            <td><button id="submitComment">Add Comment</button></td>
                                        </tr>
                                    </table>
                                    <input type="hidden" name="challenge_id" value="<?php echo $challenge_id ?>"/>
                                    <input type="hidden" name="user_id" value="<?php @session_start(); $user = $_SESSION['id'];echo $user ?>"/>
                                </form>
                </div>   
            </td>
        	<!--<td id="col-right"></td>-->
        </tr>
    </table>
    
    <style>

    .popup{
        
       
        
        

    }
</style>

    
    
</div>

<div id="bottom">
</div>

</body>

</html>
